package com.example.controller;

import com.example.bean.ApiResult;
import com.example.bean.Book;
import com.example.bean.Borrow;
import com.example.bean.User;
import com.example.service.BookService;
import com.example.service.BorrowService;
import com.example.service.UserService;
import com.example.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/borrow")
@CrossOrigin
public class BorrowController {
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;

    // 根据用户ID查询借阅信息
    @GetMapping("/selectById/{userId}")
    public ApiResult selectBorrowByUserId(@PathVariable("userId") int userId) {
        List<Borrow> res = borrowService.selectBorrowByUserId(userId);
        if (res != null && !res.isEmpty()) {
            return ApiResultHandler.buildApiResult(200, "根据用户Id查询", res);
        } else {
            return ApiResultHandler.buildApiResult(400, "无此用户借阅信息", res);
        }
    }

    //根据书籍编号查询借阅信息
    @GetMapping("/selectByBookId/{bookId}")
    public ApiResult selectBorrowByBookId(@PathVariable("bookId") String bookId) {
        List<Borrow> res = borrowService.selectBorrowById(bookId);
        if (res != null && !res.isEmpty()) {
            return ApiResultHandler.buildApiResult(200, "借阅信息", res);
        } else {
            return ApiResultHandler.buildApiResult(400, "无此书借阅信息", res);
        }
    }

    @GetMapping("/{userId}/{bookId}")
    public ApiResult selectBorrow(@PathVariable("userId") int userId,@PathVariable("bookId") String bookId) {
        Borrow res = borrowService.selectBorrow(userId,bookId);
        if (res != null) {
            return ApiResultHandler.buildApiResult(200, "借阅信息", res);
        } else {
            return ApiResultHandler.buildApiResult(400, "无此书借阅信息", res);
        }
    }

    // 查询所有借阅信息
    @GetMapping("/selectBorrow")
    public ApiResult selectBorrowAll() throws ParseException {
        List<Borrow> res = borrowService.selectBorrowAll();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0; i<res.size(); i++){
            Date b = res.get(i).getBorrowTime();
            String dataString = simpleDateFormat.format(b);
            b = simpleDateFormat.parse(dataString);
            res.get(i).setBorrowTime(b);


        }        if (res != null && !res.isEmpty()) {
            return ApiResultHandler.buildApiResult(200, "查询所有借阅信息", res);
        } else {
            return ApiResultHandler.buildApiResult(400, "查询失败", res);
        }
    }

    // 插入借阅信息
    @PostMapping("/insert")
    public ApiResult insertBorrow(@RequestBody Borrow borrow) {
        int userId = borrow.getUserId();
        User user = userService.selectUserById(userId);
        if(user.getNowBorrowNum()+1>user.getMaxBorrowNum()) return ApiResultHandler.buildApiResult(200, "超过最大借阅数量", user);
        user.setNowBorrowNum(user.getNowBorrowNum()+1);
        int i1 = userService.updateUser(user);
        if(i1==0) return ApiResultHandler.buildApiResult(200, "借阅失败", i1);
        Book book = bookService.selectBookById(borrow.getBookId());
        book.setStatus(1);
        System.out.println(book.getStatus());
        int i = bookService.updateBook(book);
        if(i == 0){
            return ApiResultHandler.buildApiResult(200, "借阅失败", i);
        }
        int res = borrowService.insertBorrow(borrow);
        if(res!=0) return ApiResultHandler.buildApiResult(200, "借阅成功", res);
        else return ApiResultHandler.buildApiResult(400, "借阅失败", res);

    }


    // 删除借阅信息
   /* @DeleteMapping("/delete/{borrowId}")
    public ApiResult deleteBorrow(@PathVariable int borrowId) {
        // 查询借阅信息对应的图书记录
       borrowService.deleteBorrow(borrowId);
       return 0;
    }*/

    // 更新借阅信息
    @PutMapping("/update")
    public ApiResult updateBorrow(@RequestBody Borrow borrow) {
        int userId = borrow.getUserId();
        User user = userService.selectUserById(userId);
        user.setNowBorrowNum(user.getNowBorrowNum()-1);
        int i = userService.updateUser(user);
        if(i==0) return ApiResultHandler.buildApiResult(400, "还书失败", user);
        Book book = bookService.selectBookById(borrow.getBookId());
        book.setStatus(0);
        int i1 = bookService.updateBook(book);
        if(i1==0) return ApiResultHandler.buildApiResult(400, "还书失败", user);
        int res = borrowService.updateBorrow(borrow);
        if(res!=0) return ApiResultHandler.buildApiResult(200, "还书成功", res);
        else return ApiResultHandler.buildApiResult(400, "还书失败", res);
    }
}
